package com.tcl.aota.manage.imp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tcl.aota.manage.OperLogManager;
import com.tcl.aota.persistent.dao.db.OperLogDAO;
import com.tcl.aota.persistent.model.AppCuref;
import com.tcl.aota.persistent.model.OperLog;

@Service("operLogManager")
public class OperLogManagerImpl implements OperLogManager {

    private static final Logger LOG = LoggerFactory.getLogger(OperLogManager.class);

    @Autowired
    private OperLogDAO          operLogDAO;

    public void recordOperLog(String ip, String account, int operType, String reason, List<AppCuref> curefList,
                              Long... appIds) {
        Date createTime = new Date();

        List<OperLog> logs = new ArrayList<OperLog>();
        OperLog operLog = null;
        for (long appId : appIds) {
            if (CollectionUtils.isNotEmpty(curefList)) {
                for (AppCuref curef : curefList) {
                    operLog = new OperLog();
                    operLog.setAppId(appId);
                    operLog.setAccount(account);
                    operLog.setOperation(operType);
                    operLog.setCreateTime(createTime);
                    operLog.setReason(reason);

                    if (StringUtils.isNotBlank(ip)) {
                        operLog.setIp(ip);
                    }
                    operLog.setCu(curef.getCuref());
                    logs.add(operLog);
                }
            } else {
                operLog = new OperLog();
                operLog.setAppId(appId);
                operLog.setAccount(account);
                operLog.setOperation(operType);
                operLog.setCreateTime(createTime);
                operLog.setReason(reason);

                if (StringUtils.isNotBlank(ip)) {
                    operLog.setIp(ip);
                }
                logs.add(operLog);
            }
        }

        batchInsertDB(logs);
    }

    public void recordOperLogByString(String ip, String account, int operType, String reason, List<String> curefList,
                                      Long... appIds) {
        Date createTime = new Date();

        List<OperLog> logs = new ArrayList<OperLog>();
        OperLog operLog = null;
        for (long appId : appIds) {
            if (CollectionUtils.isNotEmpty(curefList)) {
                for (String curef : curefList) {
                    operLog = new OperLog();
                    operLog.setAppId(appId);
                    operLog.setAccount(account);
                    operLog.setOperation(operType);
                    operLog.setCreateTime(createTime);
                    operLog.setReason(reason);

                    if (StringUtils.isNotBlank(ip)) {
                        operLog.setIp(ip);
                    }

                    operLog.setCu(curef);
                    logs.add(operLog);
                }
            } else {
                operLog = new OperLog();
                operLog.setAppId(appId);
                operLog.setAccount(account);
                operLog.setOperation(operType);
                operLog.setCreateTime(createTime);
                operLog.setReason(reason);

                if (StringUtils.isNotBlank(ip)) {
                    operLog.setIp(ip);
                }

                logs.add(operLog);
            }
        }

        batchInsertDB(logs);

    }

    private void insertDB(OperLog operLog) {
        try {
            List<OperLog> logs = new ArrayList<OperLog>();
            logs.add(operLog);
            //插入数据库
            operLogDAO.batchInsert(logs);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
    }

    private void batchInsertDB(List<OperLog> operLogs) {
        int len = 10;
        int size = operLogs.size();
        int mod = size / len;
        int offValue = size % len;

        if (offValue > 0) {
            mod++;
        }

        int startIndex = 0;
        int endIndex = 0;
        for (int i = 0; i < mod; i++) {
            startIndex = i * len;
            endIndex = (i + 1) * len;

            if (endIndex > size) {
                endIndex = size;
            }

            List<OperLog> subLogs = operLogs.subList(startIndex, endIndex);
            try {
                operLogDAO.batchInsert(subLogs);
            } catch (Exception e) {
                LOG.error(e.getMessage(), e);
            }
        }
    }

    public List<OperLog> getByAppId(Map<String, Object> param) {
        List<OperLog> result = operLogDAO.selectByAppId(param);
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }

        return result;
    }

    public int getByappIdForCount(Map<String, Object> param) {
        return operLogDAO.selectByAppIdForCount(param);
    }
}
